<template>
  <el-container style="width:100%;position:relative">
    <el-header>
      <el-page-header title="返回主页" :content="msg.title" :icon="House" @back="goBack" />
    </el-header>
    <el-container class="wrapper">
      <div class="left">
        <svg v-if="commentsNumbers == 0" t="1645967237894" class="icon" viewBox="0 0 1088 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3765" width="100" height="100">
          <path d="M1088 0v780.8l-204.8 51.2-160 179.2c-6.4 6.4-19.2 12.8-25.6 12.8-6.4 0-12.8 0-19.2-6.4-12.8-12.8-12.8-32 0-44.8l172.8-198.4 179.2-44.8V64H64v659.2h576c19.2 0 32 12.8 32 32s-12.8 32-32 32H0V0h1088zM256 384c0 38.4 25.6 64 64 64s64-25.6 64-64-25.6-64-64-64-64 25.6-64 64z m224 0c0 38.4 25.6 64 64 64s64-25.6 64-64-25.6-64-64-64-64 25.6-64 64zM832 384c0-38.4-25.6-64-64-64s-64 25.6-64 64 25.6 64 64 64 64-32 64-64z" p-id="3766"></path>
        </svg>
        <el-timeline class="comments">
          <el-timeline-item :icon="MoreFilled" type="primary" v-for="(item,index) in comments" :key="index" :timestamp="item.time">
            <span style="color: white;">{{item.createdBy}}：&nbsp;{{item.comment}}</span>
          </el-timeline-item>
        </el-timeline>
        <el-input v-model="textarea" :rows="3" type="textarea" maxlength="40" show-word-limit placeholder="Please input" />
        <TaoBtn style="width:100%;" @click="commit">发表评论</TaoBtn>
      </div>
      <div class="center">
        <div v-html="msg.content" class="markdown-body"></div>
      </div>
      <div class="right">
        <el-descriptions direction="vertical" border :column="1">
          <el-descriptions-item label="作者">{{author.nickname}}</el-descriptions-item>
          <el-descriptions-item label="职业">{{author.job}}</el-descriptions-item>
          <el-descriptions-item label="共收获点赞">
            <el-tag size="small">{{author.beLiked}}个</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="所有帖子被阅读">
            <el-tag size="small">{{author.beSeen}}次</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="你可以">
            <el-badge :value=msg.likes :max="99" class="good">
              <img src="@/assets/likes.png" alt="" @click="_praise">
            </el-badge>
            <div class="circle" v-if="nowArticleId == lookId">
              <el-icon @click="_edit">
                <edit-pen />
              </el-icon>
            </div>
            <div class="circle" v-if="nowArticleId == lookId">
              <el-icon @click="_delete">
                <delete />
              </el-icon>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-container>
    <el-backtop />
  </el-container>
</template>


<script lang="ts">
import { defineComponent, onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { EditPen, House, MoreFilled } from '@element-plus/icons-vue'
import {
  getUserInfo,
  beSeen,
  praise,
  getArticleInfo,
  deleteArticle,
  comment,
  getArticleComments,
} from '../api/index'
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js'
import 'highlight.js/styles/default.css'
import 'github-markdown-css'
import { Delete } from '@element-plus/icons'
import { ElMessage } from 'element-plus'
import getTime from '../utils/time'
import TaoBtn from '@/Tao-UI/Tao-button.vue'

// 被点击的帖子
interface interfaceRead {
  author: string
  comment: number
  content: string
  createdBy: number
  description: string
  id: number
  likes: number
  look: number
  time: string
  title: string
  type: string
}

// 这篇帖子的作者的信息
interface interfaceAuthor {
  nickname: string
  job: string
  beLiked: number
  beSeen: number
}

export default defineComponent({
  components: {
    Delete,
    EditPen,
    TaoBtn,
  },
  setup() {
    const textarea = ref<string>()

    // 发表评论
    const commit = () => {
      if (textarea.value == null) {
        ElMessage({
          showClose: true,
          message: '你倒是写点东西啊！！',
          type: 'error',
          duration: 1000,
        })
        return
      }
      const time = getTime()
      comment(
        textarea.value,
        parseInt(localStorage.getItem('nowArticles_id')),
        parseInt(localStorage.getItem('userId')),
        time
      ).then((res) => {
        textarea.value = null
        commentsNumbers.value++
        getCommits()
        ElMessage({
          message: res.data.msg,
          type: 'success',
          duration: 1000,
        })
      })
    }

    // 获取评论
    let comments = ref([])
    const getCommits = () => {
      getArticleComments(parseInt(localStorage.getItem('nowArticles_id'))).then((res) => {
        comments.value = res.data.data.reverse()
      })
    }
    const router = useRouter()
    const goBack = () => {
      router.push('/')
    }
    let msg = reactive<interfaceRead>({
      author: null,
      comment: null,
      content: null,
      createdBy: null,
      description: null,
      id: null,
      likes: null,
      look: null,
      time: null,
      title: null,
      type: null,
    })

    let author = reactive<interfaceAuthor>({
      nickname: null,
      job: null,
      beLiked: null,
      beSeen: null,
    })
    const markdown = (target) => {
      const md = new MarkdownIt({
        highlight: function (str, lang) {
          if (lang && hljs.getLanguage(lang)) {
            try {
              return (
                '<pre class="hljs"><code>' + hljs.highlight(lang, str, true).value + '</code></pre>'
              )
            } catch (_) {
              _
            }
          }
          return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>'
        },
      })
      const result = md.render(target)
      return result
    }

    let flag = ref<boolean>(true)
    let nowArticleId = ref<string>()
    let lookId = ref<string>(localStorage.getItem('userId'))
    console.log(lookId.value)

    // 点赞，并且更新数据
    const _praise = () => {
      praise(msg.id, msg.createdBy, flag.value).then((res) => {
        if (flag.value == true) {
          ElMessage({
            showClose: true,
            message: res.data.msg,
            type: 'success',
            duration: 1500,
          })
          getUserInfo(msg.createdBy).then((res) => {
            const data = res.data.data[0]
            for (let key in data) {
              author[key] = data[key]
            }
          })
          getArticleInfo(parseInt(localStorage.getItem('nowArticles_id'))).then((res) => {
            const data = res.data.data[0]
            for (const key in msg) {
              msg[key] = data[key]
            }
            msg.content = markdown(msg.content)
            beSeen(msg.id, msg.createdBy)
          })
        } else {
          getUserInfo(msg.createdBy).then((res) => {
            const data = res.data.data[0]
            for (let key in data) {
              author[key] = data[key]
            }
          })
          getArticleInfo(parseInt(localStorage.getItem('nowArticles_id'))).then((res) => {
            const data = res.data.data[0]
            for (const key in msg) {
              msg[key] = data[key]
            }
            msg.content = markdown(msg.content)
            beSeen(msg.id, msg.createdBy)
          })
        }
        flag.value = !flag.value
      })
    }

    // 删除文章
    const _delete = () => {
      deleteArticle(parseInt(localStorage.getItem('nowArticles_id'))).then((res) => {
        ElMessage({
          showClose: true,
          message: res.data.msg,
          type: 'success',
          duration: 1000,
        })
        router.push('/')
      })
    }

    // 编辑文章
    const _edit = () => {
      router.push({ path: '/write', query: { id: localStorage.getItem('nowArticles_id') } })
    }

    // 评论数，为0则显示留言图标
    let commentsNumbers = ref<number>()
    // 初始化页面数据
    onMounted(() => {
      getArticleInfo(parseInt(localStorage.getItem('nowArticles_id'))).then((res) => {
        const data = res.data.data[0]
        console.log(data)
        commentsNumbers.value = data.comment
        for (const key in msg) {
          msg[key] = data[key]
        }
        msg.content = markdown(msg.content)
        nowArticleId.value = msg.createdBy.toString()
        console.log(nowArticleId.value, lookId.value)

        getUserInfo(parseInt(nowArticleId.value)).then((res) => {
          const data = res.data.data[0]
          for (let key in data) {
            author[key] = data[key]
          }
        })
        beSeen(msg.id, msg.createdBy)
      })
      getCommits()
    })

    return {
      textarea,
      goBack,
      House,
      MoreFilled,
      msg,
      author,
      _praise,
      _edit,
      nowArticleId,
      lookId,
      _delete,
      commit,
      comments,
      commentsNumbers,
    }
  },
})
</script>
<style scoped lang="scss">
.el-header {
  display: flex;
  align-items: center;
  height: 4rem;
  width: 100%;
  box-shadow: 0px 8px 8px rgba(0, 0, 0, 0.25), 0px 4px 8px rgba(0, 0, 0, 0.8);
  background: #282a2e;
  position: fixed;
  z-index: 999;
}
:deep .el-page-header__content {
  color: white;
}
:deep .el-page-header__title {
  color: white;
}
// :deep .el-icon svg {
//   color: white;
// }
.wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
  position: relative;
  height: 100vh;
  .left {
    width: 15rem;
    padding-top: 4.5rem;
    position: relative;
    .icon {
      position: absolute;
      top: 30%;
      left: 30%;
    }
    .comments {
      height: 80%;
      overflow-y: auto;
      .el-timeline-item {
        margin: 0.5rem;
      }
    }
  }
  .center {
    background-color: #fff;
    position: relative;
    width: 45rem;
    padding-top: 4rem;
  }
  .right {
    padding-top: 4rem;
    width: 15rem;
    .el-descriptions {
      margin-top: 0.5rem;
      margin-left: 0.5rem;
    }
    .good {
      margin: 1rem 1rem 0 0.5rem;
      float: left;
      cursor: pointer;
    }
    .circle {
      width: 4rem;
      height: 4rem;
      background-position: 50%;
      background-repeat: no-repeat;
      border-radius: 50%;
      display: flex;
      flex: 1;
      justify-content: center;
      align-items: center;
      float: left;
      cursor: pointer;
      img {
        width: 2rem;
        height: 2rem;
      }
      .el-icon {
        font-size: 2rem;
        color: rgb(156, 165, 174);
      }
    }
  }
}

.markdown-body {
  padding: 20px;
  min-width: 200px;
  // max-width: 900px;
  font-size: 12px;
  h2 {
    font-size: 18px;
    margin: 1em 0 15px;
    padding-top: 0.8em;
    padding-bottom: 0.8em;
  }
  h3 {
    font-size: 14px;
    margin: 22px 0 16px;
  }
  h4 {
    font-size: 13px;
    margin: 20px 0 16px;
  }
  h5 {
    font-size: 12px;
    margin: 16px 0 16px;
    font-weight: 700;
  }
  p {
    font-size: 12px;
    line-height: 24px;
    color: #666666;
    margin-top: 0px;
    margin: 8px 0;
    margin: 14px 0 14px;
  }
  pre {
    background-color: #eee;
    margin-bottom: 8px;
    margin-top: 8px;
    margin: 12px 0 12px;
  }
  blockquote {
    margin-bottom: 8px;
    margin-top: 8px;
    margin: 14px 0 14px;
    background-color: #eee;
    padding: 16px 16px;
  }
  tr {
    background-color: #f5f5f5;
  }
  code {
    background-color: #eee;
  }
  ul,
  ol,
  li {
    list-style: unset;
    font-size: 12px;
    line-height: 20px;
    color: #666666;
    margin-top: 0px;
    margin: 8px 0;
  }
  blockquote {
    border-color: #48b6e2;
  }
  table {
    display: table;
    width: 100%;
    max-width: 100%;
    margin-bottom: 20px;
  }
}
</style>